﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Threading;

namespace Astrid_Desktop
{
    public partial class frmSplashScreen : Form
    {
        static frmSplashScreen splashScreen = null;
        private double opacitiyIncrement = .05;
        private double opacitiyDecrement = .1;
        private const int TIMER_INTERVAL = 50;
        static Thread splashScreenThread = null;

        public frmSplashScreen()
        {
            InitializeComponent();
            this.Opacity = 0.0;
            tmrFade.Interval = TIMER_INTERVAL;
            tmrFade.Start();
        }

        private void frmSplashScreen_Load(object sender, EventArgs e)
        {
            lblInfo.Text = "Astrid Desktop is not in any way shape or form associated with Todoroo, Inc\nother than the fact that it uses their API.";
        }

        private static void ShowForm()
        {
            splashScreen = new frmSplashScreen();
            Application.Run(splashScreen);

        }

        public static void CloseForm()
        {
            if (splashScreen != null)
            {
                splashScreen.opacitiyIncrement = -splashScreen.opacitiyDecrement;
            }

            splashScreen = null;
            splashScreenThread = null;
        }

        public static void ShowSplashScreen()
        {
            if (splashScreenThread != null)
            {
                return;
            }
            splashScreenThread = new Thread(new ThreadStart(frmSplashScreen.ShowForm));
            splashScreenThread.IsBackground = true;
            splashScreenThread.SetApartmentState(ApartmentState.STA);
            splashScreenThread.Start();
        }

        private void tmrFade_Tick(object sender, EventArgs e)
        {
            if (opacitiyIncrement > 0)
            {
                if (this.Opacity < 1)
                {
                    this.Opacity += opacitiyIncrement;
                }
            }
            else
            {
                if (this.Opacity > 0)
                {
                    this.Opacity += opacitiyIncrement;
                }
                else
                {
                    this.Close();
                    tmrFade.Enabled = false;
                }
            }
        }
    }
}
